package com.xunchi.haichuan.configurator.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.codehaus.plexus.util.StringUtils;

import com.netai.safedoc.utility.TransactionWrapper;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.xunchi.haichuan.audit.service.LogService;
import com.xunchi.haichuan.audit.service.LogServiceImpl;
import com.xunchi.haichuan.configurator.model.SystemConfig;
import com.xunchi.haichuan.configurator.model.SystemConfigs;
import com.xunchi.haichuan.configurator.service.SystemConfigService;
import com.xunchi.haichuan.configurator.service.SystemConfigServiceImpl;
import com.xunchi.haichuan.utility.MiscUtil;

/**
 * 作者：kongxb<br>
 * 模块名称：系统配置模块<br>
 * 功能描述：系统配置管理<br>
 * 维护记录：2009.11.27, kongxb 创建该类
 */
public class SystemConfigAction extends ActionSupport {

	private static final long serialVersionUID = -8776847694180370178L;

	private Map<String, Object> item = new HashMap<String, Object>();

	private List<Map<String, Object>> items = new ArrayList<Map<String, Object>>();
	private int totalCount;

	private boolean success = true;
	private String msg = "";

	private SystemConfigService systemConfigService;
	private LogService logService;

	public String execute() throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("recordsPerPage", SystemConfigs.recordsPerPage);
		setItem(map);

		return Action.SUCCESS;
	}

	/**
	 * 更新系统配置
	 * 
	 * @return
	 * @throws Exception
	 */
	public String update() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();

		systemConfigService = (SystemConfigService) TransactionWrapper.decorate(new SystemConfigServiceImpl(), true);
		logService = (LogService) TransactionWrapper.decorate(new LogServiceImpl(), true);

		String items = request.getParameter("items");
		JSONArray jsonArray = JSONArray.fromObject(items);
		for (Iterator iter = jsonArray.iterator(); iter.hasNext();) {
			JSONObject item = (JSONObject) iter.next();
			systemConfigService.update(item.getString("id"), item.getString("value"));
		}

		SystemConfigs.setSystemConfigs();

		return Action.SUCCESS;
	}

	/**
	 * 列表系统配置
	 * 
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception {
		systemConfigService = (SystemConfigService) TransactionWrapper.decorate(new SystemConfigServiceImpl());
		logService = (LogService) TransactionWrapper.decorate(new LogServiceImpl(), true);

		try {
			List<SystemConfig> list = systemConfigService.getSystemConfigs();
			totalCount = systemConfigService.getTotalCount();

			for (Iterator<SystemConfig> iter = list.iterator(); iter.hasNext();) {
				SystemConfig systemConfig = iter.next();

				if (StringUtils.equals(systemConfig.getConfId(), "maxFileSize")
						|| StringUtils.equals(systemConfig.getConfId(), "allowedFileType")
						|| StringUtils.equals(systemConfig.getConfId(), "deniedFileType")) {
					continue;
				}

				Map<String, Object> map = new HashMap<String, Object>();
				map.put("id", systemConfig.getConfId());
				map.put("name", MiscUtil.replaceMsgKey(systemConfig.getName()));
				map.put("descript", MiscUtil.replaceMsgKey(systemConfig.getDescript()));
				String value = systemConfig.getValue();
				try {
					map.put("value", Integer.parseInt(value));
				} catch (NumberFormatException e) {
					try {
						map.put("value", Float.parseFloat(value));
					} catch (NumberFormatException e1) {
						if ("true".equalsIgnoreCase(value) || "false".equalsIgnoreCase(value)) {
							map.put("value", Boolean.parseBoolean(value));
						} else {
							map.put("value", value);
						}
					}
				}
				items.add(map);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return Action.SUCCESS;
	}

	/**
	 * @return the item
	 */
	public Map<String, Object> getItem() {
		return item;
	}

	/**
	 * @param item
	 *            the item to set
	 */
	public void setItem(Map<String, Object> item) {
		this.item = item;
	}

	/**
	 * @return the items
	 */
	public List<Map<String, Object>> getItems() {
		return items;
	}

	/**
	 * @return the totalCount
	 */
	public int getTotalCount() {
		return totalCount;
	}

	public boolean isSuccess() {
		return success;
	}

	public String getMsg() {
		return msg;
	}

}
